﻿Public Class UCQLTinDang
    Private TinDangBUS As TinDangBUS
    Private TinDangDAO As TinDangDAO
    Dim connect As New connect()
    Private Sub UCQLTinDang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadcmbDonVi()
        LoadcmbQuan()
        LoadcmbLoaiPhong()
        LoadTin()
    End Sub
    Private Sub LoadTin()
        dgvTinChothue.ClearSelection()
        TinDangBUS = New TinDangBUS
        Dim TinDangList As New List(Of TinDangDTO)
        TinDangList = TinDangBUS.GetData()
        dgvTinChothue.DataSource = TinDangList
        dgvTinChothue.ClearSelection()
    End Sub
    Private Sub LoadcmbDonVi()
        Dim ds As New DataSet
        ds = connect.LoadDonVi("select MADV,TENDV from DONVI")
        With cmbDonVi
            .DataSource = ds.Tables("DONVI")
            .DisplayMember = "TENDV"
            .ValueMember = "MADV"
        End With
    End Sub
    Private Sub LoadcmbQuan()
        Dim ds As New DataSet
        ds = connect.LoadQuan("select MAQUAN,TENQUAN from QUAN")
        With cmbQuan
            .DataSource = ds.Tables("QUAN")
            .DisplayMember = "TENQUAN"
            .ValueMember = "MAQUAN"
        End With
    End Sub
    Private Sub LoadcmbLoaiPhong()
        Dim ds As New DataSet
        ds = connect.LoadLoaiPhong("select MALP,TENLP from LOAIPHONG")
        With cmbLoai
            .DataSource = ds.Tables("LOAIPHONG")
            .DisplayMember = "TENLP"
            .ValueMember = "MALP"
        End With
    End Sub
    Private Sub LamMoi()
        LoadTin()
        txtMaTin.Text = ""
        txtTieuDe.Text = ""
        txtDienTich.Text = ""
        txtGiaTien.Text = ""
        txtSucChua.Text = ""
        txtMaKH.Text = ""
        txtMaNV.Text = ""
        cmbDonVi.SelectedValue = -1
        cmbQuan.SelectedValue = -1
        cmbLoai.SelectedValue = -1
        dtNgayDang.Text = ""
        cbBiXoa.Checked = False
        cbCoInternet.Checked = False
        cbDaThue.Checked = False
        cbGanBus.Checked = False
        cbGanCho.Checked = False
        btnCapNhat.Enabled = False
        btnXoa.Enabled = False
    End Sub
    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()
    End Sub

    Private Sub dgvTinChothue_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles dgvTinChothue.SelectionChanged
        txtMaTin.Text = dgvTinChothue.CurrentRow.Cells(0).Value.ToString()
        txtTieuDe.Text = dgvTinChothue.CurrentRow.Cells(1).Value.ToString()
        dtNgayDang.Text = dgvTinChothue.CurrentRow.Cells(2).Value.ToString()
        cmbLoai.SelectedValue = Convert.ToInt32(dgvTinChothue.CurrentRow.Cells(3).Value)
        cmbQuan.SelectedValue = Convert.ToInt32(dgvTinChothue.CurrentRow.Cells(4).Value)
        txtMaKH.Text = dgvTinChothue.CurrentRow.Cells(5).Value.ToString()
        txtMaNV.Text = dgvTinChothue.CurrentRow.Cells(6).Value.ToString()
        txtDienTich.Text = dgvTinChothue.CurrentRow.Cells(7).Value.ToString()
        txtSucChua.Text = dgvTinChothue.CurrentRow.Cells(8).Value.ToString()
        cbGanCho.Checked = Convert.ToBoolean(dgvTinChothue.CurrentRow.Cells(9).Value)
        cbCoInternet.Checked = Convert.ToBoolean(dgvTinChothue.CurrentRow.Cells(10).Value)
        cbGanBus.Checked = Convert.ToBoolean(dgvTinChothue.CurrentRow.Cells(11).Value)
        txtGiaTien.Text = dgvTinChothue.CurrentRow.Cells(12).Value.ToString()
        cmbDonVi.SelectedValue = dgvTinChothue.CurrentRow.Cells(13).Value.ToString()
        cbDaThue.Checked = Convert.ToBoolean(dgvTinChothue.CurrentRow.Cells(14).Value)
        cbBiXoa.Checked = Convert.ToBoolean(dgvTinChothue.CurrentRow.Cells(15).Value)
        btnCapNhat.Enabled = True
        btnXoa.Enabled = True
    End Sub
  
    Private Sub btnCapNhat_Click(sender As System.Object, e As System.EventArgs) Handles btnCapNhat.Click
        Dim maTin As String = txtMaTin.Text.Trim()
        Dim maKH As String = txtMaKH.Text.Trim()
        Dim maNV As String = txtMaNV.Text.Trim()
        Dim tieuDe As String = txtTieuDe.Text.Trim()
        Dim ngay As DateTime = dtNgayDang.Value
        Dim ngayDang As String = ngay.Year().ToString() + "-" + ngay.Month().ToString() + "-" + ngay.Day().ToString()
        Dim maLP As String = cmbLoai.SelectedValue.ToString().Trim()
        Dim maQuan As String = cmbQuan.SelectedValue.ToString().Trim()
        Dim dienTich As String = txtDienTich.Text.Trim()
        Dim sucChua As String = txtSucChua.Text.Trim()
        Dim giaTien As String = txtGiaTien.Text.Trim()
        Dim maDV As String = cmbDonVi.SelectedValue.ToString().Trim()
        Dim ganCho As String = cbGanCho.Checked.ToString()
        Dim ganBus As String = cbGanBus.Checked.ToString()
        Dim coInternet As String = cbCoInternet.Checked.ToString()
        Dim daThue As String = cbDaThue.Checked.ToString()
        If (cbDaThue.Checked) Then
            connect.ExecSql("update PHIEUTHUE set BIXOA='True' where MATD='" + maTin + "'")
        Else
            connect.ExecSql("update PHIEUTHUE set BIXOA='False' where MATD='" + maTin + "'")
        End If
        Dim biXoa As String = cbBiXoa.Checked.ToString()
        Dim sql As String = "update TINDANG set TIEUDE=N'" + tieuDe + "', NGAYDANG='" + ngayDang + "', MALP=" + maLP + ",MANV='" + maNV + "', MAQUAN=" + maQuan + " ,MAKH='" + maKH + "', DIENTICH=" + dienTich + ", SUCCHUA=" + sucChua + ", GANCHO='" + ganCho + "', INTERNET='" + coInternet + "',GANBUS='" + ganBus + "', GIATIEN=" + giaTien + ", MADV='" + maDV + "', DATHUE='" + daThue + "', BIXOA='" + biXoa + "' where MATD='" + maTin + "'"
        connect.ExecSql(sql)
        LoadTin()
        LamMoi()
    End Sub

    Private Sub btnXoa_Click(sender As System.Object, e As System.EventArgs) Handles btnXoa.Click
        If (txtMaTin.Text <> "") Then
            Dim sql As String = "select * from PHIEUTHUE where MATD='" + txtMaTin.Text + "'"
            Dim dt As New DataTable
            dt = connect.GetData(sql)
            If (dt.Rows.Count() > 0) Then
                Dim result = MessageBox.Show("Không xoá được tin này vì đã có người thuê. Bạn có muốn đánh dấu Xoá không?", "Thông báo", MessageBoxButtons.YesNo)
                If result = DialogResult.No Then
                    'Me.Close()
                ElseIf result = DialogResult.Yes Then
                    sql = "update TINDANG set BIXOA='True' where MATD='" + txtMaTin.Text + "'"
                    connect.ExecSql(sql)
                    MessageBox.Show("Đánh dấu xoá thành công!")
                    LoadTin()
                    LamMoi()
                End If
            Else
                Dim sqldelete As String = "delete TINDANG where MATD='" + txtMaTin.Text + "'"
                connect.ExecSql(sqldelete)
                MessageBox.Show("Xoá thành công!")
                LoadTin()
                LamMoi()
            End If
        End If
    End Sub

 
End Class
